package com.tnxst.phexamination.adapter.follow;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;


import com.tnxst.phexamination.R;
import com.tnxst.phexamination.utils.StringUtils;
import com.tnxst.phexamination.utils.Utils;
import com.tnxst.phexamination.vo.DrugVo;

import java.util.List;

public class AddFollowDrugAdapter extends RecyclerView.Adapter<AddFollowDrugAdapter.ViewHolder> {

    private Context context;

    private List<DrugVo> followDrugs;

    private ChangedData changedData;


    public AddFollowDrugAdapter(List<DrugVo> followDrugs) {
        this.followDrugs = followDrugs;
    }

    public void setDrugVos(List<DrugVo> followDrugs){
        this.followDrugs = followDrugs;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = View.inflate(parent.getContext(), R.layout.door_record_drug_item, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DrugVo followDrug = followDrugs.get(position);
        if (StringUtils.isNotNull(followDrug.getCmDrugName()))
            holder.name.setText(followDrug.getCmDrugName());
        if (StringUtils.isNotNull(followDrug.getDailyTimes()))
            holder.dailyTimes.setText(String.valueOf(followDrug.getDailyTimes()));
        if (StringUtils.isNotNull(followDrug.getEachDose()))
            holder.eachDose.setText(String.valueOf(followDrug.getEachDose()));
        if (StringUtils.isNotNull(followDrug.getRemark()))
            holder.company.setText(followDrug.getRemark());
        if (StringUtils.isNotNull(followDrug.getRemark1()))
            holder.remark.setText(followDrug.getRemark1());
        if (StringUtils.isNotNull(followDrug.getCmDrugUsage())){
            switch (followDrug.getCmDrugUsage()){
                case "1":
                    holder.usage.setText("口服");
                    break;
                case "2":
                    holder.usage.setText("静脉注射");
                    break;
                case "3":
                    holder.usage.setText("皮下注射");
                    break;
            }
        }

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
                View view = View.inflate(context, R.layout.door_record_drug_add, null);
                alertDialog
                        .setView(view)
                        .create();
                final AlertDialog show = alertDialog.show();
                EditText name = view.findViewById(R.id.editText28);
                EditText dailyTimes = view.findViewById(R.id.editText32);
                EditText eachDose = view.findViewById(R.id.editText31);
                EditText company = view.findViewById(R.id.editText29);
                EditText remark = view.findViewById(R.id.editText65);
                RadioGroup usage = view.findViewById(R.id.radioGroup);
                Button save = view.findViewById(R.id.button21);
                Button cancel = view.findViewById(R.id.button20);
                if (StringUtils.isNotNull(followDrug.getCmDrugName()))
                    name.setText(followDrug.getCmDrugName());
                if (StringUtils.isNotNull(followDrug.getDailyTimes()))
                    dailyTimes.setText(String.valueOf(followDrug.getDailyTimes()));
                if (StringUtils.isNotNull(followDrug.getEachDose()))
                    eachDose.setText(String.valueOf(followDrug.getEachDose()));
                if (StringUtils.isNotNull(followDrug.getRemark()))
                    company.setText(followDrug.getRemark());
                save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (StringUtils.isNotNull(name.getText().toString())
                                && StringUtils.isNotNull(dailyTimes.getText().toString())
                                && StringUtils.isNotNull(eachDose.getText().toString())
                                && StringUtils.isNotNull(company.getText().toString())){
                            if ("mg".equals(company.getText().toString()) ||
                                    "ml".equals(company.getText().toString()) ||
                                    "g".equals(company.getText().toString()) ||
                                    "片".equals(company.getText().toString()) ||
                                    "粒".equals(company.getText().toString()) ||
                                    "袋".equals(company.getText().toString()) ||
                                    "瓶".equals(company.getText().toString()) ||
                                    "支".equals(company.getText().toString()) ||
                                    "盒".equals(company.getText().toString()) ||
                                    "U".equals(company.getText().toString())){
                                followDrug.setCmDrugName(name.getText().toString());
                                followDrug.setDailyTimes(dailyTimes.getText().toString());
                                followDrug.setEachDose(eachDose.getText().toString());
                                followDrug.setRemark(company.getText().toString());
                                followDrug.setRemark1(remark.getText().toString());
                                switch (usage.getCheckedRadioButtonId()){
                                    case R.id.radioButton44:
                                        followDrug.setCmDrugUsage("1");
                                        break;
                                    case R.id.radioButton46:
                                        followDrug.setCmDrugUsage("2");
                                        break;
                                    case R.id.radioButton47:
                                        followDrug.setCmDrugUsage("3");
                                        break;
                                }
                                changedData.changedData();
                                show.dismiss();
                            }else {
                                Utils.Toast(context,"单位必须填写mg,ml,g,片,粒,袋,瓶,支,盒,U中的一个！");
                            }

                        }else {
                            Utils.Toast(context,"不能为空！");
                        }
                    }
                });
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        show.dismiss();
                    }
                });
            }
        });
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                followDrugs.remove(position);
                changedData.changedData();
            }
        });
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        TextView dailyTimes;
        TextView eachDose;
        TextView company;
        TextView delete;
        TextView remark;
        TextView usage;
        View view;

        public ViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.textView130);
            dailyTimes = itemView.findViewById(R.id.textView188);
            eachDose = itemView.findViewById(R.id.textView189);
            company = itemView.findViewById(R.id.textView190);
            view = itemView;
            delete = itemView.findViewById(R.id.textView);
            remark = itemView.findViewById(R.id.textView432);
            usage = itemView.findViewById(R.id.textView435);
        }
    }

    @Override
    public int getItemCount() {
        return followDrugs.size();
    }

    public interface ChangedData{
        void changedData();
    }

    public void setOnClickChangedData(ChangedData changedData){
        this.changedData = changedData;
    }


}
